#include<bits/stdc++.h>
#define ONLINE_JUDGE
#define ll long long
#define ull unsigned long long
using namespace std;

const int N = 110, INF = 0x3f3f3f3f;

int n, sum;
int team[N], w[N], pre[N];
int dp[N][510][510];

void solve()
{
    cin >> n;
    for (int i = 1;i <= n;i++) {
        cin >> team[i] >> w[i];
        sum += w[i];
        pre[i] = pre[i - 1] + w[i];
    }
    if (sum % 3) { cout << -1;return; }
    sum = sum / 3;

    memset(dp, 0x3f, sizeof dp);
    dp[0][0][0] = 0;
    for (int i = 1;i <= n;i++)
        for (int a = 0;a <= sum;a++)
            for (int b = 0;b <= sum;b++) {
                if (a - w[i] >= 0) dp[i][a][b] = min(dp[i][a][b], dp[i - 1][a - w[i]][b] + (team[i] != 1));
                if (b - w[i] >= 0)dp[i][a][b] = min(dp[i][a][b], dp[i - 1][a][b - w[i]] + (team[i] != 2));
                if (pre[i] - a - b - w[i] >= 0)dp[i][a][b] = min(dp[i][a][b], dp[i - 1][a][b] + (team[i] != 3));
            }
    if (dp[n][sum][sum] != INF)
        cout << dp[n][sum][sum] << '\n';
    else cout << -1 << '\n';
}

signed main()
{
    // ios::sync_with_stdio(0);
    // cin.tie(0);cout.tie(0);
#ifndef ONLINE_JUDGE
    std::istringstream in(R"()");
    std::cin.rdbuf(in.rdbuf());
#endif
    int T = 1;
    //cin>>T;
    for (int i = 1;i <= T;i++) {
        solve();
    }
}